﻿/// <reference path="../classes.js" />
/// <reference path="qunit-1.11.0.js" />

// Methos for testing:
//  - initialize
//  - addMachine
//  - addVehicle
//  - renderPartial
//  - renderFull

(function () {

    module("ProductionBuilding.init");
    test("Initialization of not rented", function () {
        // initialize: function (name, area, retailPrice, rented)
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        equal(building.name, name, "Name");
        equal(building.area, area, "Area");
        equal(building.retailPrice, price, "Price");
        equal(building.rented, rented, "Is building rented");
        equal(building.yearlyRent, "N/a", "Yearly rent");
    });
    test("Initialization of rented", function () {
        // initialize: function ($super, name, area, retailPrice, rented, yearlyRent)
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = true,
           yearlyRent = 60000;

        var building = new ProductionBuilding(name, area, price, rented, yearlyRent);
        equal(building.name, name, "Name");
        equal(building.area, area, "Area");
        equal(building.retailPrice, price, "Price");
        equal(building.rented, rented, "Is building rented");
        equal(building.yearlyRent, yearlyRent, "Yearly rent");
    });

    module("addMachine");
    test("Add zero machines", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        equal(building.machines.length, 0);
    });
    test("Add one machine", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        //function (years, guarantee, amortizationPeriod)
        var machine = new Machine(10, 15, 20);
        building.addMachine(machine);
        equal(building.machines.length, 1);
    });
    test("Adding server and uncategorized", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        //function (years, guarantee, amortizationPeriod)
        var machine = new Machine(10, 15, 20);
        //server ($super, years, guarantee, amortizationPeriod, mileage, OS, usage)
        var server = new Server(machine.yearsOld, machine.guarantee,
            machine.amortizationPeriod, 12, "Ubutu", "data processing");
        building.addMachine(server);
        building.addMachine(machine);
        equal(building.machines.length, 2);
    });
    test("Adding company as machine, should throw exception", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        var company = new Company("Kubrat", 3000000);
        throws(function () {
            building.addMachine(company);
        },"Only machines can be added");
        equal(building.machines.length, 0);
    });

    module("addVehicles");
    test("Adding one vehicle", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        var vehicle = new Vehicle(10, 3, 5, 8, "yes", "Van");
        building.addVehicle(vehicle);
        equal(building.vehicles.length, 1);
    });
    test("Adding machine as vehicle, should throw exception", function () {
        var name = "Production 2",
           area = 560,
           price = 300000,
           rented = false;

        var building = new ProductionBuilding(name, area, price, rented);
        var machine = new Machine(3, 4, 19);
        var vehicle = new Vehicle(10, 3, 5, 8, "yes", "Van");
        throws(function () {
            building.addVehicle(machine);
        }, "Exception has been thrown. Only vehicles can be added.");
        equal(building.vehicles.length, 0, "No invalid machines are added.");
    });

    module("renderPartial");
    test("Render empty building", function () {
        var building = new ProductionBuilding("Production building", 350, 1000000, false);
        var fixture = document.getElementById("qunit-fixture");
        var container = building.renderPartial();
        $(fixture).append(container);
        var divs = fixture.getElementsByTagName('div');
        equal(divs.length, 2);
        var lists = fixture.getElementsByTagName("ul");
        equal(lists.length, 0);
    });
    test("Render building with one machine", function () {
        var building = new ProductionBuilding("Production building", 350, 1000000, false);
        var machine = new Machine(3, 4, 19);
        building.addMachine(machine);

        var fixture = document.getElementById("qunit-fixture");
        var container = building.renderPartial();
        $(fixture).append(container);

        var lists = fixture.getElementsByTagName('ul');
        equal(lists.length, 1);
        var divs = fixture.getElementsByTagName('div');
        equal(divs.length, 3);
    });
    test("Render building with one machine and worker", function () {
        var building = new ProductionBuilding("Production building", 350, 1000000, false);
        var machine = new Machine(3, 4, 19);
        building.addMachine(machine);
        var worker = new Worker("John", "Bon Jovi", 32, 12);
        building.addWorker(worker);

        var fixture = document.getElementById("qunit-fixture");
        var container = building.renderPartial();
        $(fixture).append(container);

        var lists = fixture.getElementsByTagName('ul');
        equal(lists.length, 2);
        var divs = fixture.getElementsByTagName('div');
        equal(divs.length, 4);
    });

    module("renderFull");
    test("Render empty building", function () {
        var building = new ProductionBuilding("Production building", 350, 1000000, false);

        var fixture = document.getElementById("qunit-fixture");
        var container = building.renderFull();
        $(fixture).append(container);

        var divs = fixture.getElementsByTagName('div');
        equal(divs.length, 8);
    });
    test("Render building with 2 machines", function () {
        var building = new ProductionBuilding("Production building", 350, 1000000, false);
        var machine = new Machine(3, 4, 19);
        var machine1 = new Machine(3, 4, 19);

        building.addMachine(machine);
        building.addMachine(machine1);

        var fixture = document.getElementById("qunit-fixture");
        var container = building.renderFull();
        $(fixture).append(container);

        var divs = fixture.getElementsByTagName('div');
        equal(divs.length, 8, "Divs");
    });


}());
